package com.e3.sysmg.security.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.sysmg.security.model.RoleModel;
import com.e3.template.tgf.GFHelper;
import com.e3.web.core.model.ModelHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.e3.web.widget.client.MyMenuItem;
import com.extjs.gxt.ui.client.data.ModelComparer;
import com.extjs.gxt.ui.client.data.ModelKeyProvider;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.menu.Menu;

public class RoleHelper extends GFHelper<RoleModel>{
	
	public static RoleHelper INSTANCE = new RoleHelper();
	
	private static ModelHelper<RoleModel> modelHelper = new ModelHelper<RoleModel>();
	
	private String[] notNullFieldNames = {RoleModel.CODE,RoleModel.NAME};
	
	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		
		boolean isNotNull = isNotNullField(name);
		if(name.equals(RoleModel.CODE)){
			formField = MyFormFieldFactory.createStringField(name,dispName,20,isNotNull);
		}
		else if(name.equals(RoleModel.NAME)){
			formField = MyFormFieldFactory.createStringField(name,dispName,50,isNotNull);
		}
		else if(name.equals(RoleModel.DESCRIPTION)){
			formField = MyFormFieldFactory.createTextAreaField(name,dispName,100,isNotNull);
		}
		else if(name.equals(RoleModel.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}
		return formField;
	}
	
	public Menu getGridContextMenu(SelectionListener<MenuEvent> listener){
		Menu menu = new Menu();
		
		MyMenuItem item = new MyMenuItem(RoleStateMachine.SHOW_SELFMODULES_ACTION);
		item.addSelectionListener(listener);
		menu.add(item);
		
		item = new MyMenuItem(RoleStateMachine.SHOW_ALLMODULES_ACTION);
		item.addSelectionListener(listener);
		menu.add(item);
		
		item = new MyMenuItem(RoleStateMachine.NEW_ACTION);
		item.addSelectionListener(listener);
		menu.add(item);
				
		item = new MyMenuItem(RoleStateMachine.MODIFY_ACTION);
		item.addSelectionListener(listener);
		menu.add(item);
		
		item = new MyMenuItem(RoleStateMachine.DELETE_ACTION);
		item.addSelectionListener(listener);
		menu.add(item);
		
		return menu;
	}
	
	public Menu getParentRoleGridContextMenu(SelectionListener<MenuEvent> listener){
		Menu menu = new Menu();
		
		MyMenuItem item = new MyMenuItem(RoleStateMachine.SHOW_SELFMODULES_ACTION);
		item.addSelectionListener(listener);
		menu.add(item);
		
		item = new MyMenuItem(RoleStateMachine.SHOW_ALLMODULES_ACTION);
		item.addSelectionListener(listener);
		menu.add(item);
		
		return menu;
	}
	
	public List<ColumnConfig> getColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);
		
		ColumnConfig column = new ColumnConfig();
		String name = RoleModel.CODE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(150);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = RoleModel.NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(150);   
	    configs.add(column); 
	    
	    column = new ColumnConfig();
		name = RoleModel.DESCRIPTION;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(300);   
	    configs.add(column); 
		
		return configs;
	}

	
	public ModelComparer<RoleModel> getModelComparer(){
		return modelHelper.getModelComparer();
	}
	
	public ModelKeyProvider<RoleModel> getModelKeyProvider(){
		return modelHelper.getModelKeyProvider();
	}
	
	public String getFieldDisplayName(String code) {
		return RoleFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFieldNames;
	}

	@Override
	public RoleModel createModelInstance() {
		return new RoleModel();
	}
}
